/*
 * Copyright (C) 2017 grandcentrix GmbH
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.grandcentrix.thirtyinch.sample;

import net.grandcentrix.thirtyinch.TiActivity;
import net.grandcentrix.thirtyinch.TiLog;
import net.grandcentrix.thirtyinch.logginginterceptor.LoggingInterceptor;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Text;
import rx.Observable;
import rx.subjects.PublishSubject;


public class HelloWorldActivity extends TiActivity<HelloWorldPresenter, HelloWorldView> implements HelloWorldView {

    private Button button;
    private Text output;
    private Text uptime;
    private PublishSubject<Void> publishSubject = PublishSubject.create();

    {
        addBindViewInterceptor(new LoggingInterceptor());
    }


    @Override
    public HelloWorldPresenter providePresenter() {
        return new HelloWorldPresenter();
    }

    @Override
    protected void onStart(Intent intent) {
        TiLog.i("HelloWorldActivity", "onStart");
        setUIContent(ResourceTable.Layout_activity_hello_world);

        button = (Button) findComponentById(ResourceTable.Id_button);
        output = (Text) findComponentById(ResourceTable.Id_output);
        uptime = (Text) findComponentById(ResourceTable.Id_uptime);

//        if (savedInstanceState == null) {
        getFractionManager().startFractionScheduler()
                .replace(ResourceTable.Id_fragment_container, new SampleFragment())
                .submit();
//        }

        findComponentById(ResourceTable.Id_recreate).setEnabled(false);
//        findComponentById(ResourceTable.Id_recreate).setClickedListener((v) -> {
//            // recreate();
//            TiLog.i("HelloWorldActivity", "stopAbility");
//            terminateAbility();
//        });

        findComponentById(ResourceTable.Id_start_fragment_lifecycle_test).setClickedListener((v) -> {
            Intent _intent = new Intent();
            _intent.setOperation(new Intent.OperationBuilder()
                    .withDeviceId("")
                    .withBundleName("net.grandcentrix.thirtyinch.sample")
                    .withAbilityName("net.grandcentrix.thirtyinch.sample.fragmentlifecycle.FragmentLifecycleActivity")
                    .build());
            startAbility(_intent);
        });

        findComponentById(ResourceTable.Id_start_viewpager_test).setClickedListener((v) -> {
            Intent _intent = new Intent();
            _intent.setOperation(new Intent.OperationBuilder()
                    .withDeviceId("")
                    .withBundleName("net.grandcentrix.thirtyinch.sample")
                    .withAbilityName("net.grandcentrix.thirtyinch.sample.fragmentlifecycle.LifecycleViewPagerActivity")
                    .build());
            startAbility(_intent);
        });

        super.onStart(intent);
    }

    @Override
    public Observable<Void> onButtonClicked() {
//        return RxView.clicks(button);
        button.setClickedListener(v -> publishSubject.onNext((Void) null));
        //TODO chad how to free publishSubject
        return publishSubject;
    }

    @Override
    protected void onStop() {
        super.onStop();
        publishSubject.onCompleted();
    }

    @Override
    public void showPresenterUpTime(Long uptime) {
        this.uptime.setText(String.format("Presenter alive for %ss", uptime));
    }

    @Override
    public void showText(String text) {
        output.setText(text);
    }
}
